<template>
    <el-dialog title="分配角色" :visible="openAuth" width="80%" @close="close" v-dialogDrag append-to-body>
        <h4 class="form-header h4">基本信息</h4>
        <el-form :model="form" label-width="80px">
            <el-row>
                <!-- <el-col :span="8" :offset="2">
                    <el-form-item label="用户昵称" prop="nickName">
                        <el-input v-model="form.nickName" disabled />
                    </el-form-item>
                </el-col> -->
                <el-col :span="8" :offset="2">
                    <el-form-item label="用户名称" prop="userName">
                        <el-input v-model="form.userName" disabled />
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>

        <h4 class="form-header h4">角色信息</h4>
        <el-table v-loading="loading" :row-key="getRowKey" @row-click="clickRow" ref="roleRef"
            @selection-change="handleSelectionChange" :data="roles.slice((pageNum - 1) * pageSize, pageNum * pageSize)">
            <el-table-column label="序号" width="55" type="index" align="center">
                <template #default="scope">
                    <span>{{ (pageNum - 1) * pageSize + scope.$index + 1 }}</span>
                </template>
            </el-table-column>
            <el-table-column type="selection" :reserve-selection="true" width="55"></el-table-column>
            <el-table-column label="角色编号" align="center" prop="roleId" />
            <el-table-column label="角色名称" align="center" prop="roleName" />
            <el-table-column label="权限字符" align="center" prop="roleKey" />
            <el-table-column label="创建时间" align="center" prop="createTime" width="180">
                <template #default="scope">
                    <span>{{ timeFormat(scope.row.createTime) }}</span>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination v-if="total > 0" :total="total" background layout="total,sizes, prev, pager, next"
            :page-size="pageSize" :current-page="pageNum" :page-sizes="[10, 20, 30, 40, 50]" @size-change="pageSizeChange"
            @current-change="selectData" style="float:right;"></el-pagination>
        <!-- <pagination v-show="total > 0" :total="total" v-model:page="pageNum" v-model:limit="pageSize" /> -->
        <template #footer>
            <div class="dialog-footer">
                <el-button type="primary" @click="submitForm()">确 定</el-button>
                <el-button @click="close">取 消</el-button>
            </div>
        </template>
    </el-dialog>
</template>
<script lang="ts" setup name="AuthRole">
import { getAuthRole, updateAuthRole } from "@/requests/api/sys-user";
import { timeFormat } from "@/utils/time";
import { Message } from "element-ui";
import { nextTick, ref, Ref, watch } from "vue";

const props = defineProps({
    openAuth: {
        type: Boolean,
        default: false
    },
    authID: {
        type: String,
        default: "0"
    }
});
const loading = ref(true);
const total = ref(0);
const pageNum = ref(1);
const pageSize = ref(10);
const roleIds = ref([]);
const roles = ref([]);
const formInit = {
    nickName: undefined,
    userName: undefined,
    userId: undefined
}
const form = ref(Object.assign({}, formInit));
const emit = defineEmits(['closeAuth']);
watch(() => props.openAuth, (n) => {
    if (n) {
        getUserAuthRole();
    } else {
        emit('closeAuth');
    }

})
const roleRef: Ref<any> = ref(null);
/** 单击选中行数据 */
function clickRow(row: any) {
    roleRef.value.toggleRowSelection(row);
};
/** 多选框选中数据 */
function handleSelectionChange(selection: any) {
    roleIds.value = selection.map((item: any) => item.roleId);
};
/** 保存选中的数据编号 */
function getRowKey(row: any) {
    return row.roleId;
};
/** 关闭按钮 */
function close() {
    emit('closeAuth');
};
/** 提交按钮 */
function submitForm() {
    const userId = form.value.userId;
    const rIds = roleIds.value.join(",");
    updateAuthRole({ userId: userId, roleIds: rIds }).then(() => {
        Message.success("授权成功");
        close();
    });
};

const getUserAuthRole = () => {
    // const userId = route.params && route.params.userId;
    const userId = props.authID;
    if (userId) {
        loading.value = true;
        getAuthRole(userId).then((res: any) => {
            if (res.code !== 200) {
                Message.error(res.message);
                form.value = Object.assign({}, formInit);
                roles.value = [];
            } else {
                form.value = res.user;
                roles.value = res.roles;
                total.value = roles.value.length;
            }

            nextTick(() => {
                roles.value.forEach((row: any) => {
                    if (row.flag) {
                        roleRef.value.toggleRowSelection(row);
                    }
                });
            });
            loading.value = false;
        });
    }
};
</script>
<style lang="scss" scoped></style>